package module.main.view.mainInterface.friendListPanel
{
	import com.darwin.darwinUI.containers.ContainerBase;
	import com.darwin.darwinUI.controls.Button;
	import com.darwin.darwinUI.controls.Label;
	import com.darwin.darwinUI.controls.List;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.events.UIEvent;
	import com.darwin.darwinUI.general.DarwinFont;
	import com.darwin.darwinUI.general.TextFieldFactory;
	import com.darwin.darwinUI.manager.UIManager;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.ui.Mouse;
	
	import main.App;
	
	import module.main.view.ResourceConst;
	
	/**
	 * 	好友列表面板
	 * 	@author killsoon
	 */	
	public class FriendListPanel extends UIComponent
	{
		private var panelBg:UIComponent;
		private var addFriendButton:Button;
		private var leftArrow:Button;
		private var rightArrow:Button;
		private var friendList:List;
		private var pageTxt:Label;
		private var _data:Object;
		
		public function FriendListPanel()
		{
			initView();
			initListener();
		}
		
		private function initView():void
		{
			drawBg();
			createAddFriend();
			createArrow();
			createPageIndex();
			createList();
		}
		
		private function initListener():void
		{
			addFriendButton.addEventListener(MouseEvent.CLICK, addFriend);
			
			leftArrow.addEventListener(MouseEvent.CLICK, changePage);
			rightArrow.addEventListener(MouseEvent.CLICK, changePage);
			
			friendList.addEventListener(UIEvent.LIST_ROLL_COMP, rollPageComplete);
			friendList.addEventListener(Event.ADDED_TO_STAGE, addedToStage);
			
			addEventListener(Event.REMOVED_FROM_STAGE, removeHandler);
		}
			
		private function removeHandler(e:Event):void
		{
			removeAllChild();
		}
		
		/**
		 *	设置列表数据 
		 * 	@param value
		 * 
		 */
		public function set data(value:Array):void
		{
			_data = value;
			if(value)
				friendList.source = value;
			else
				friendList.source = [];
		}
		
		/**
		 *	绘制背景 
		 */		
		public function drawBg():void
		{
			panelBg = new UIComponent();
			panelBg.backgroundSkin = ResourceConst.FRIENDLIST_BG;
			panelBg.backgroundAuto = true;
			addChildAt(panelBg, 0);
		}
		
		/**
		 *	添加好友
		 */		
		private function addFriend(e:MouseEvent):void
		{
		}
		
		private function addedToStage(e:Event):void
		{
			rollPageComplete();
		}
		
		/**
		 *	翻页结束改变当前页数 
		 */		
		private function rollPageComplete(e:UIEvent = null):void
		{
//			pageTxt.x = 36 + (67 - (pageTxt.width + 5)) / 2;
			if(friendList.pageLength == 0)
			{
//				pageTxt.text = "0/0";
				pageTxt.text = UIManager.getLanguageManager().getText("friendlist_page", [0, 0]);
			}
			else
			{
//				pageTxt.text = friendList.currentPage + 1 + "/" + friendList.pageLength;
				pageTxt.text = UIManager.getLanguageManager().getText("friendlist_page", [friendList.currentPage + 1, friendList.pageLength]);
			}
			
			pageTxt.x = 41 + (67 - (pageTxt.width + 5)) / 2 - 2;
			
			if(friendList.currentPage != 0 && friendList.currentPage != friendList.pageLength - 1)
			{
				leftArrow.filters = [];
				rightArrow.filters = [];
				leftArrow.mouseChildren = true;
				rightArrow.mouseChildren = true;
				leftArrow.mouseEnabled = true;
				rightArrow.mouseEnabled = true;
			}
			else
			{
				if(friendList.currentPage == 0 && (friendList.currentPage == friendList.pageLength - 1 || friendList.pageLength == 0))
				{
					leftArrow.filters = [new ColorMatrixFilter([0.3086, 0.6094, 0.082, 0, 0, 0.3086, 
						0.6094, 0.082, 0, 0, 0.3086, 0.6094, 0.082, 0, 0, 0, 0, 0, 1, 0])];
					leftArrow.mouseChildren = false;
					leftArrow.mouseEnabled = false;
					rightArrow.filters = [new ColorMatrixFilter([0.3086, 0.6094, 0.082, 0, 0, 0.3086,
						0.6094, 0.082, 0, 0, 0.3086, 0.6094, 0.082, 0, 0, 0, 0, 0, 1, 0])];
					rightArrow.mouseChildren = false;
					rightArrow.mouseEnabled = false;
				}
				else
				{
					if(friendList.currentPage == 0)
					{
						//	添加黑白滤镜
						leftArrow.filters = [new ColorMatrixFilter([0.3086, 0.6094, 0.082, 0, 0, 0.3086, 
												0.6094, 0.082, 0, 0, 0.3086, 0.6094, 0.082, 0, 0, 0, 0, 0, 1, 0])];
						rightArrow.filters = [];
						leftArrow.mouseChildren = false;
						leftArrow.mouseEnabled = false;
						rightArrow.mouseChildren = true;
						rightArrow.mouseEnabled = true;
					}
					else
					{
						rightArrow.filters = [new ColorMatrixFilter([0.3086, 0.6094, 0.082, 0, 0, 0.3086,
												0.6094, 0.082, 0, 0, 0.3086, 0.6094, 0.082, 0, 0, 0, 0, 0, 1, 0])];
						leftArrow.filters = [];
						leftArrow.mouseChildren = true;
						leftArrow.mouseEnabled = true;
						rightArrow.mouseChildren = false;
						rightArrow.mouseEnabled = false;
					}
				}
			}
		}
	
		/**
		 *	翻页 
		 */		
		private function changePage(e:MouseEvent):void
		{
			switch(e.currentTarget)
			{
				case leftArrow:
					friendList.rollToPage(friendList.currentPage - 1, 0);
					break;
				case rightArrow:
					friendList.rollToPage(friendList.currentPage + 1, 0);
					break;
				default:
					break;
			}
		}

		/**
		 *	创建添加好友按钮 
		 */	
		private function createAddFriend():void
		{
			addFriendButton = new Button();
			addFriendButton.backgroundSkin = ResourceConst.ADD_FRIEND_BUTTON;
			addFriendButton.backgroundAuto = true;
			addFriendButton.x = 15;
			addFriendButton.y = -6;
			addChild(addFriendButton);
		}
		
		/**
		 *	创建翻页左右箭头 
		 */		
		private function createArrow():void
		{
			leftArrow = new Button();
			leftArrow.backgroundSkin = ResourceConst.LEFT_ARROW;
			leftArrow.backgroundAuto = true;
			leftArrow.x = 26;
			leftArrow.y = 250;
			addChild(leftArrow);
			
			rightArrow = new Button();
			rightArrow.backgroundSkin = ResourceConst.LEFT_ARROW;
			rightArrow.backgroundAuto = true;
			rightArrow.mouseChildren = false;
			rightArrow.scaleX = -1;
			rightArrow.x = 113;
			rightArrow.y = 250;
			addChild(rightArrow);
		}
		/**
		 * 	创建好友列表
		 */		
		private function createList():void
		{
			friendList = new List(FriendListItem, 125, 41, 0, 2);
			friendList.width = 125;
			friendList.height = 250;
			friendList.x = 7;
			friendList.y = 35;
			addChild(friendList);
		}
		
		/**
		 *	创建页数显示 
		 */		
		private function createPageIndex():void
		{
			pageTxt = TextFieldFactory.getInstance().getLabel("friendlist_page");
			pageTxt.mouseEnabled = false;
//			pageTxt.x = 54;
			pageTxt.y = 251;
			pageTxt.autoSize = true;
//			pageTxt.widthAuto = false;
//			pageTxt.width = 35;
//			pageTxt.heightAuto = false;
//			pageTxt.height = 20;
			pageTxt.x = (67 - (pageTxt.width + 5)) / 2 - 2;
			addChild(pageTxt);
		}
	}
}